package com.tfj.mvp.tfj.live;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.tfj.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.tfj.comm.bean.Constant;
import com.tfj.mvp.base.BaseFragment;
import com.tfj.mvp.tfj.live.bean.LiveBean;
import com.tfj.mvp.tfj.live.bean.TagBean;
import com.tfj.utils.GridSpacingItemDecoration;
import com.tfj.utils.SysUtils;
import com.tfj.widget.RoundAngleImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.BindView;

/**
 * Created by HuangQiang on 2017/12/21.
 *
 * @author HuangQiang
 * @github https://github.com/HuangQiang
 */

public class VLiveFragment extends BaseFragment<PLiveImpl> implements CLive.IVLive {


    @BindView(R.id.recycleView_tag)
    RecyclerView recycleViewTag;
    @BindView(R.id.recycleView_livedata)
    RecyclerView recycleViewLivedata;
    @BindView(R.id.linearlayout_nodata)
    LinearLayout linearlayoutNodata;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int checkPosition = 0;
    public static VLiveFragment newInstance() {
        VLiveFragment fragment = new VLiveFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        //标签
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycleViewTag.setLayoutManager(linearLayoutManager);
        recycleViewTag.setAdapter(quickAdapter_tag);
        List<TagBean> beans = new ArrayList<>();
        beans.add(new TagBean("全部"));
        beans.add(new TagBean("别墅豪宅"));
        beans.add(new TagBean("优惠促销"));
        beans.add(new TagBean("正在直播"));
        quickAdapter_tag.replaceData(beans);
        //直播的数据
        StaggeredGridLayoutManager recyclerViewLayoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerViewLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recycleViewLivedata.setLayoutManager(recyclerViewLayoutManager);
        recycleViewLivedata.setAdapter(quickAdapter_live);
        recycleViewLivedata.addItemDecoration(new GridSpacingItemDecoration(2,10,true));
        recycleViewLivedata.setItemAnimator(new DefaultItemAnimator());
        List<LiveBean> liveBeans  = new ArrayList<>();
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        liveBeans.add(new LiveBean());
        quickAdapter_live.replaceData(liveBeans);


    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_live;
    }

    @Override
    public void createPresenter() {
        mPresenter = new PLiveImpl(mContext, this);
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }
    private QuickAdapter_Tag  quickAdapter_tag = new QuickAdapter_Tag();
    public class QuickAdapter_Tag extends BaseQuickAdapter<TagBean, BaseViewHolder> {
        public QuickAdapter_Tag() {
            super(R.layout.item_tag);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final TagBean item) {
            RelativeLayout rl_out = viewHolder.getView(R.id.rl_out);
            TextView textView_name = viewHolder.getView(R.id.textView_name);
            textView_name.setText(item.getName());
            textView_name.setTextColor(viewHolder.getPosition()==checkPosition?Color.parseColor("#ffffff"):Color.parseColor("#ffbfc2cc"));
            rl_out.setBackgroundResource(viewHolder.getPosition()==checkPosition?R.drawable.shape_background_main_2:R.drawable.shape_background_gray_2);
            addDisposable(RxView.clicks(rl_out).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                    .subscribe(o -> {
                        checkPosition = viewHolder.getPosition();
                        quickAdapter_tag.notifyDataSetChanged();
                    }));
        }
    }
    private  QuickAdapter_Live quickAdapter_live = new QuickAdapter_Live();
    public class QuickAdapter_Live extends BaseQuickAdapter<LiveBean, BaseViewHolder> {
        public QuickAdapter_Live() {
            super(R.layout.item_live);
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, final LiveBean item) {
            RelativeLayout rl_top = viewHolder.getView(R.id.rl_top);
            ViewGroup.LayoutParams layoutParams = rl_top.getLayoutParams();
            if(viewHolder.getAdapterPosition()%3==0){
                layoutParams.height = SysUtils.dip2px(getActivity(),140);
            }else{
                layoutParams.height = SysUtils.dip2px(getActivity(),100);
            }
            rl_top.setLayoutParams(layoutParams);
        }
    }
}
